package cc.rengu.igas.bomp.core.service.trans;

import cc.rengu.igas.bomp.common.enums.RespCodeEnum;
import cc.rengu.igas.bomp.core.service.base.ManageService;
import cc.rengu.igas.bomp.facade.request.GeneratorTermValidCodeRequest;
import cc.rengu.igas.bomp.facade.response.GeneratorTermValidCodeResponse;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.BizResponse;
import cc.rengu.oltp.utility.util.ConvertUtil;
import cc.rengu.oltp.utility.util.RandomUtil;
import cc.rengu.oltp.utility.util.RedisUtil;
import cc.rengu.oltp.utility.util.StringUtil;

/**
 * 生成终端装机验证码
 * Created by moxm
 */
public class GeneratorTermValidCodeService extends ManageService {
    @Override
    protected Object bizBeforeCust() throws Exception {
        GeneratorTermValidCodeRequest generatorTermValidCodeRequest = new GeneratorTermValidCodeRequest();
        ConvertUtil.convertOutput(generatorTermValidCodeRequest);
        return generatorTermValidCodeRequest;
    }

    @Override
    protected BizResponse bizProcessCust(Object request) throws Exception {
        GeneratorTermValidCodeRequest generatorTermValidCodeRequest = (GeneratorTermValidCodeRequest) request;
        GeneratorTermValidCodeResponse generatorTermValidCodeResponse = new GeneratorTermValidCodeResponse();
        /*机构标识*/
        String instId = generatorTermValidCodeRequest.getHeader().getInstId();
        /*用户标识*/
        String userId = generatorTermValidCodeRequest.getUserId();
        /*商户编号*/
        String mchntNo = generatorTermValidCodeRequest.getMchntNo();
        /*终端编号*/
        String termNo = generatorTermValidCodeRequest.getTermNo();
        /*验证码有效期*/
        String expiryTime = generatorTermValidCodeRequest.getExpiryTime();
        /*必填性校验*/
        if (StringUtil.isEmptyOrNull(userId) || StringUtil.isEmptyOrNull(mchntNo) || StringUtil.isEmptyOrNull(termNo) || StringUtil.isEmptyOrNull(expiryTime)) {
            rglog.error("生成终端装机验证码交易必填性校验失败！");
            throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
        }
        /*生成6位随机数验证码*/
        String validCode = RandomUtil.randomNumStr(6);
        /* 将随机验证码存储到Redis缓存中 */
        RedisUtil.onceSet(instId + mchntNo + termNo + "TMKCODE", validCode, Integer.parseInt(expiryTime));
        rglog.info("终端装机验证码保存到Redis缓存成功：" + validCode);
        /* 组装应答报文 */
        generatorTermValidCodeResponse.setValidCode(validCode);
        generatorTermValidCodeResponse.setExpiryTime(expiryTime);
        BizResponse bizResponse = new BizResponse();
        bizResponse.setRspSysId(AppParamConstant.SYS_ID);
        bizResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
        bizResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
        bizResponse.setResult(generatorTermValidCodeResponse);
        return bizResponse;
    }

    @Override
    protected void bizAfterCust(Object response) throws Exception {
        BizResponse bizResponse = (BizResponse) response;
        GeneratorTermValidCodeResponse generatorTermValidCodeResponse = (GeneratorTermValidCodeResponse) bizResponse.getResult();
        ConvertUtil.convertInput(generatorTermValidCodeResponse);
    }
}
